﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Webapi.EFCore.Common.Attributes;
using Webapi.EFCore.Common.Helper;
using Webapi.EFCore.IRepository;
using Webapi.EFCore.IRepository.Base;
using Webapi.EFCore.IService;
using Webapi.EFCore.Model;
using Webapi.EFCore.Model.Attributes;
using Webapi.EFCore.Model.Enity;
using Webapi.EFCore.Model.ViewModel;
using Webapi.EFCore.Service.Base;

namespace Webapi.EFCore.Service
{
    /// <summary>
    /// User服务 
    /// </summary>
    [Service]
    public class UserService : BaseService<User>, IUserService
    {
        private readonly IUserRepository userDal;
        private readonly IRoleUserRepository roleUserDal;
        private readonly IUnitOfWork unitOfWork;
        private readonly IMapper imapper;

        /// <summary>
        ///  构造函数,注入仓储
        /// </summary>
        /// <param name="userRepository"></param>
        /// <param name="roleUserRepository"></param>
        /// <param name="_uow"></param>
        public UserService(IUserRepository userRepository,
            IRoleUserRepository roleUserRepository,
            IUnitOfWork _uow,
            IMapper mapper) : base(userRepository)
        {
            userDal = userRepository;
            roleUserDal = roleUserRepository;
            unitOfWork = _uow;
            imapper = mapper;
        }

        /// <summary>
        /// 获取用户数量
        /// </summary>
        /// <returns></returns>
        public async Task<int> GetCount()
        {
            return await userDal.GetCount();
        }

        /// <summary>
        /// 写入实体
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<MessageModel<CurdModel>> AddRoleBat(UserRoleModel model)
        {
            //先插入主表
            User user = new User();
            user.UserId = model.UserId;
            user.UserName = model.UserName;
            user.Age = model.Age;
            userDal.Add(user);
            userDal.SaveChanges();

            //再批量插入关系表
            string[] strrid = model.RoleId.Split(',');
            foreach (string rid in strrid)
            {
                RoleUser roleUser = new RoleUser();
                roleUser.UserId = user.UserId;
                roleUser.RoleId = int.Parse(rid);

                roleUserDal.Add(roleUser);
                //unitOfWork.GetDbContext().Set<RoleUser>().Add(roleUser);
                //(unitOfWork.GetDbContext() as EFCoreDbContext).RoleUser.Add(roleUser);
            }

            //统一提交【工作单元】
            var result = await unitOfWork.SaveChangesAsync();
            if (result > 0)
            {
                return HttpResult.Ok();
            }
            else
            {
                return HttpResult.DbError("添加失败!");
            }
        }


        /// <summary>
        /// 获取用户详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<UserViewModel> GetUserDetails(int id)
        {
            User userinfo = await userDal.GetBYID(id);
            if (userinfo != null)
            {
                //UserViewModel model = new UserViewModel();
                //model.UserId = userinfo.UserId;
                //model.UserName = userinfo.UserName;
                //model.Age = userinfo.Age;
                UserViewModel model = imapper.Map<UserViewModel>(userinfo);

                model.Address = "北京市xx区xx小区";
                model.Birthday = "1996-06-26";
                model.Phone = "13888888888";
                return model;
            }
            else
            {
                return null;
            }
        }
    }
}
